#!/user/bin/python
# -*- coding: UTF-8 -*-

# if __name__ == '__main__':
#     from Tkinter import *
#     from tkMessageBox import *
#
#     def answer():
#         showerror("Answer", "Sorry, no answer available")
#
#     def callback():
#         if askyesno('Verify', 'Really quit?'):
#             showwarning('Yes', 'Not yet implemented')
#         else:
#             showinfo('No', 'Quit has been cancelled')
#
#     Button(text='Quit', command=callback).pack(fill=X)
#     Button(text='Answer', command=answer).pack(fill=X)
#     mainloop()

import os
from Tkinter import *
from tkMessageBox import *
import time

class CheckLinuxMem :
    isNext = True;
'''
check linux mem by alan
Date:   2017/12/08
'''
if __name__ == '__main__':
    # isNext = True;
    while CheckLinuxMem.isNext:
        time.sleep(5);
        tt2 = os.popen("free | grep 'Mem'| awk '{print $7}'");
        aa = tt2.read();
        # print aa;
        bb = int(aa);
        if bb < 405000:
            root = Tk()  # 初始化Tk()
            def callbackIsNext():
                if askyesno('确定', '是否确定退出检测?'):
                    print 'test yes';
                    CheckLinuxMem.isNext = False;
                    root.destroy()
                    # sys.exit(0);
                else:
                    print 'test no';
                    CheckLinuxMem.isNext = True;
                    root.destroy()
            root.title("内存警告!")  # 设置窗口标题
            # root.geometry("200x100")  # 设置窗口大小 注意：是x 不是*
            root.resizable(width=True, height=False)  # 设置窗口是否可以变化长/宽，False不可变，True可变，默认为True
            l = Label(text="内存过大!", font=("Arial", 14),foreground="red", width=8, height=3)
            l.pack()  # 这里的side可以赋值为LEFT  RTGHT TOP  BOTTOM
            Button(text='确定', command=callbackIsNext, width=10, height=1).pack(side=BOTTOM)

            # curWidth = root.winfo_reqwidth()  # get current width
            # curHeight = root.winfo_height()  # get current height
            curWidth = 200;
            curHeight = 100;
            scnWidth, scnHeight = root.maxsize()  # get screen width and height
            # now generate configuration information
            tmpcnf = '%dx%d+%d+%d' % (curWidth, curHeight,
                                      (scnWidth - curWidth) / 2, (scnHeight - curHeight) / 2)
            root.geometry(tmpcnf)
            # 窗口自动置顶
            root.wm_attributes('-topmost',1)
            root.mainloop()  # 进入消息循环

